﻿using Desktop.Controls;
using Desktop.Library;
using Microsoft.Win32;
using System;
using System.ComponentModel;
using System.Reflection;
using System.Security.Principal;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;

namespace Desktop
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        #region Constructors

        public MainWindow()
        {
            InitializeComponent();

            LoadRibbons();
            LoadIdentity();
        }

        #endregion

        #region Methods

        private void LoadRibbons()
        {
            string[] files = System.IO.Directory.GetFiles(System.IO.Directory.GetCurrentDirectory(), "*.dll");
            foreach (string filename in files)
            {
                try
                {
                    Assembly asm = Assembly.LoadFile(filename);
                    foreach (Type type in asm.GetTypes())
                    {
                        if (type.BaseType == typeof(Ribbon))
                        {
                            ribbonBar.Items.Add(Activator.CreateInstance(type, new object[] { application }) as Ribbon);
                        }
                    }
                }
                catch (Exception)
                {
                    // Unable to search the DLL for one reason or another.
                }
            }
        }

        private void LoadIdentity()
        {
            AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
            loggedInAsLabel.DataContext = (Thread.CurrentPrincipal.Identity) as WindowsIdentity;
        }

        #endregion

        #region Event Handlers

        private void Window_Closing(object sender, CancelEventArgs e)
        {
            application.RaiseEvent(new RoutedEventArgs(Desktop.Controls.Application.ClosingEvent, application));
        }

        #endregion
    }
}